﻿/// <summary>
/// 功能描述    ：UrlQuery  
/// 创 建 者    ：txb
/// 创建日期    ：2018/11/26 15:57:50 
/// 最后修改者  ：txb
/// 最后修改日期：2018/11/26 15:57:50 
/// </summary>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace OnHook.Common.UtilsHelper
{
    /// <summary>
    /// UrlQuery
    /// </summary>
    public class UrlHelper
    {
        /// <summary>
        /// 实体转换成Http query参数
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="isFilterEmpty">是否过滤空字符串</param>
        /// <returns></returns>
        public static string ToUrlQuery(Object obj, bool isFilterEmpty = true)
        {
            var sb = new StringBuilder();
            PropertyInfo[] propertyInfos = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
            foreach (PropertyInfo property in propertyInfos)
            {
                if (property.PropertyType.IsValueType)//值类型
                {
                    if (property.PropertyType.IsEnum)
                    {
                        continue;
                        //  sb.AppendFormat("{0}={1}&", property.Name, HttpUtility.UrlDecode(dataTimeStr));
                    }
                    object objVal = property?.GetValue(obj, null)!;
                    if (objVal == null)
                    {
                        continue;
                    }
                    if (isFilterEmpty && property?.PropertyType == typeof(string))
                    {
                        if (objVal.ToString() == string.Empty)
                        {
                            continue;
                        }
                    }
                    if (property?.PropertyType == typeof(DateTime) || property?.PropertyType != typeof(DateTime?))//自定义的实体对象不处理
                    {

                        string dataTimeStr = string.Empty;
                        if (objVal != null)
                        {
                            dataTimeStr = objVal.ToString()??"";
                        }

                        sb.AppendFormat("{0}={1}&", property?.Name??"", Uri.EscapeDataString(dataTimeStr));
                    }
                    else
                    {
                        sb.AppendFormat("{0}={1}&", property.Name, objVal);
                    }

                }
                else
                {
                    if (property.PropertyType != typeof(string))//自定义的实体对象不处理
                    {
                        continue;
                    }
                    object objVal = property.GetValue(obj, null)!;
                    objVal ??= "";
                    sb.AppendFormat("{0}={1}&", property?.Name, Uri.EscapeDataString(objVal.ToString()??""));
                }

            }
            return sb.ToString().TrimEnd('&');
        }

        /// <summary>
        /// 拼接A标签跳转后的地址
        /// </summary>
        /// <param name="sourceUrl"></param>
        /// <param name="href"></param>
        /// <returns></returns>
        public static string ToTargetUrl(string sourceUrl,string href)
        {
            var uri = new Uri(sourceUrl);
            string url;
            string segment = string.Empty;
            for(int i = 0; i < uri.Segments.Length - 1; i++)
            {
                segment += uri.Segments[i];
            }

            string port = string.Empty;
            if(uri.Port != 80)
            {
                port = $":{uri.Port}";
            }
            if(href.StartsWith('/'))
            {
                url = $"{uri.Scheme}://{uri.Host}{port}{href}";
            }
            else
            {
                url = $"{uri.Scheme}://{uri.Host}{port}{segment}/{href}";
            }

            return url;
        }
    }
}
